package com.feng.vhr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feng.vhr.common.RespBean;
import com.feng.vhr.pojo.Employee;
import com.feng.vhr.pojo.Empsalary;
import com.feng.vhr.pojo.Salary;
import com.feng.vhr.response.REmployee;
import com.feng.vhr.service.IEmployeeService;
import com.feng.vhr.service.IEmpsalaryService;
import com.feng.vhr.service.ISalaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    private ISalaryService salaryService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IEmpsalaryService empsalaryService;

    /**
     * 查询所有的工资账套
     *
     * @return
     */
    @GetMapping("/sob")
    public RespBean sob() {
        List<Salary> list = salaryService.list();
        return RespBean.ok("信息查询成功", list);
    }

    /**
     * 添加账套
     *
     * @param salary
     * @return
     */
    @PostMapping("/sob")
    public RespBean addSob(@RequestBody Salary salary) {
        System.out.println(salary);
        boolean save = salaryService.save(salary);
        if (save) {
            return RespBean.ok("添加账套成功！");
        }
        return RespBean.error("添加账套失败！");
    }


    /**
     * 修改账套
     *
     * @param salary
     * @return
     */
    @PutMapping("/sob")
    public RespBean updateSob(@RequestBody Salary salary) {
        boolean update = salaryService.updateById(salary);
        if (update) {
            return RespBean.ok("修改账套成功！");
        }
        return RespBean.error("修改账套失败！");
    }

    /**
     * 删除账套
     * @param id
     * @return
     */
    @DeleteMapping("/sob/{id}")
    public RespBean deleteSob(@PathVariable int id) {
        boolean remove = salaryService.removeById(id);
        if (remove) {
            return RespBean.ok("删除账套成功！");
        }
        return RespBean.error("删除账套失败！");
    }


    /**
     * 分页查询员工账套信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/sobcfg")
    public RespBean page(int page, int size) {

        PageHelper.startPage(page,size);
        List<REmployee> allEmpSalaryAndDepartment = employeeService.getAllEmpInfo(new Employee(),null);
        PageInfo<REmployee> rEmployeePageInfo = new PageInfo<>(allEmpSalaryAndDepartment);
        return RespBean.ok("员工信息查询成功",rEmployeePageInfo);
    }

    /**
     * 修改员工账套
     * @param eid
     * @param sid
     * @return
     */
    @PutMapping("/sobcfg")
    public RespBean updateSalary(int eid,int sid){
        QueryWrapper<Empsalary> wrapper = new QueryWrapper<>();
        wrapper.eq("eid",eid);
        Empsalary empsalary = empsalaryService.getOne(wrapper);
        empsalary.setSid(sid);
        boolean update = empsalaryService.updateById(empsalary);
        if (update){
            return RespBean.ok("修改员工账套成功");
        }
        return RespBean.error("修改员工账套失败");
    }

    /**
     * 查询账套列表信息
     * @return
     */
    @GetMapping("/sobcfg/salaries")
    public RespBean salaries() {
        List<Salary> list = salaryService.list();
        return RespBean.ok("信息查询成功",list);
    }
}
